<template>
	<view>
		<view class="uni-header">
			<view class="uni-group">
				<view class="uni-title"></view>
				<view class="uni-sub-title"></view>
				<el-button class="uni-button" type="success" size="mini" @click="navigateTo('/pages/configs/add')">新增</el-button>
			</view>
		</view>
		<lh-configuration-form
			ref="form"
			:type="'fill'"
			:disabled="false"
			:formTemplate="formTemplate"
			:formValue="formValue"
			:isCard="true"
			@setCode="setCode"
		></lh-configuration-form>
		<view class="uni-button-group">
			<button type="primary" class="uni-button" style="width: 100px;" @click="save">提交</button>
			<navigator v-if="false" open-type="navigateBack" style="margin-left: 15px;"><button class="uni-button" style="width: 100px;">返回</button></navigator>
		</view>
	</view>
</template>

<script>
const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'configs';
import template from '@/addFile.js';
import lhConfigurationForm from '@/components/lh-configurationForm/lh-configurationForm.vue';
// import lbPicker from '@/components/lb-picker/index.vue'
export default {
	components: {
		lhConfigurationForm
	},
	data() {
		return {
			value1: 'B',
			list1: [
				{
					label: '选项一',
					value: 'A'
				},
				{
					label: '选项二',
					value: 'B'
				}
			],

			formTemplate: [],
			formValue: {
			}
		};
	},
	created() {
		console.log(dbCollectionName);
		this.loadData();
	},
	methods: {
		navigateTo(url, clear) {
			// clear 表示刷新列表时是否清除当前页码，true 表示刷新并回到列表第 1 页，默认为 true
			uni.navigateTo({
				url,
				events: {
					refreshData: () => {
						this.loadData(clear);
					}
				}
			});
		},
		async loadData() {
			let tabMenu = [];
			const db = uniCloud.database();
			await db
				.collection('configs')
				.where({ textName: 'tab_group' })
				.get({
					getOne: true
				})
				.then(res => {
					tabMenu = res.result.data.options;
				});
			await db
				.collection('configs')
				.get()
				.then(res => {
					const tempList=res.result.data
					let formValue={}
					tabMenu.forEach(item=>{
						item.object=[];
						tempList.forEach(child=>{
							if(child.group===item.id){
								item.object.push(child)
							}
							formValue[child.textName]=child.value
						})
					})
					this.formValue=formValue
					this.formTemplate = tabMenu;
				})
				.catch(err => {
					console.log(err); // 打印错误码
				});
		},
		setCode(value) {
			//value的值为配置中的 getfield:'phoneNumber'有关
			//获取验证码
			if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(value)) {
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '手机号不正确'
				});
				return false;
			}

			this.$refs.form.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
		},
		save() {
			let result = this.$refs.form.submit();
			if (result.checkFlag) {
				const db = uniCloud.database();
				const dbCollectionName = 'configs';
				const dbtable = db.collection(dbCollectionName);
				let i;
				let resObj
				for (i in result.value) {
					if(i!='tab_group'){
						if(i=='comment_item'||i=='user_cancel_reason'||i=='paramedics_cancel_reason'){
							const val=result.value[i]
							let dbres=	dbtable.where({ textName: i }).update({ values:val.split(',')  })
						}else{
							let dbres=	dbtable.where({ textName: i }).update({ value: result.value[i] })
						}
					}
				}
				uni.showToast({
					title: '更新成功'
				});
			} else {
				uni.showToast({
					title: "---->"+result.message,
					icon: 'none'
				});
			}
		},
		handleTap(name) {
			this.$refs[name].show();
		},
		handleChange(item) {
			console.log('change::', item);
		},
		handleConfirm(item) {
			console.log('confirm::', item);
		},
		handleCancel(item) {
			console.log('cancel::', item);
		}
	}
};
</script>

<style>
.button {
	margin: 14px 10px 10px 10px;
	background-color: #6ba1ff;
	color: #ffffff;
}
/* #ifdef APP-NVUE */

.uni-icon {
	font-family: uniicons;
	font-weight: normal;
}
/* #endif */
/* #ifndef APP-NVUE */

/* uni-icon */

.uni-icon {
	font-family: uniicons;
	font-size: 24px;
	font-weight: normal;
	font-style: normal;
	line-height: 1;
	display: inline-block;
	text-decoration: none;
	-webkit-font-smoothing: antialiased;
}

.uni-icon.uni-active {
	color: #007aff;
}
/* #endif */
</style>
